<?


class Controller extends CoderBase 
{

	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function Controller()
	{
		$this->CoderBase(__CLASS__) ;
		$this->Initialize() ;
	}
	
	/**
	 * 初始化相关的 MVC 对象
	 * 
	 * @access	public
	 * @return	void
	 */
	public function Initialize()
	{
		// 消息队列
		$aMessageQueue = new JCAT_MessageQueue() ;
		JCAT_MessageDispatcher::RegisterMessageQueue($aMessageQueue,MSGMVC) ;
		JCAT_MessageDispatcher::RegisterMessageQueue($aMessageQueue,MSGMVC_OK) ;
		JCAT_MessageDispatcher::RegisterMessageQueue($aMessageQueue,MSGMVC_BAD) ;
		JCAT_MessageDispatcher::RegisterMessageQueue($aMessageQueue,MSGMVC_DEBUG) ;
		$this->SetMessageQueue($aMessageQueue) ;
		
		// 消息输出器
		JCAT_Package::ImportClass('JCAT_MessageGtkDialogPrinter',JCAT::PACKAGE_MSG) ;
		$aMessageQueuePrinter = new JCAT_MessageGtkDialogPrinter($aMessageQueue) ;
		$aMessageQueue->AddPrinter($aMessageQueuePrinter) ;
		
		// 主文档
		$aDoc = new ControllerDoc() ;
		$this->AddDocument($aDoc,'MainDoc') ;
		
		// 子控制器
		$aSubDoc = $aDoc->GetObjectChildrenDoc() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aSubDoc,'JCATMVCObjectContainerDoc') ;
		$aSubController = new JCATMVCObjectContainer($this,$aSubDoc) ;
		$this->AddController($aSubController,'MVCContainer') ;

		// 子视图
		$aView = new ControllerView($this) ;
		$aView->SetDocument($aDoc) ;
		JCAT_View::RegisterGlobalName($aView,'MainView') ;
		$this->AddView($aView) ;
		
		// 父类
		parent::Initialize($aDoc) ;
	}

	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	string
	 */
	public function GetDialogTitle()
	{
		return JCAT_Language::Sentence('JCAT 控制器') ;
	}
	
	

	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function SetPath( $sPath )
	{
		$aView = JCAT_View::GetViewByGlobalName('ControllerView') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aView,'ControllerView') ;

		$aView->SetPath( $sPath ) ;
	}

	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function GetPath()
	{
		$aDoc = $this->GetDocument('MainDoc') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aDoc) ;
		
		return $aDoc->Get('ControllerPath') ;
	}
	
	/**
	 * 
	 *
	 * @access	public
	 * @return	void
	 */
	public function MainRun($arrArgv=array())
	{$arrArgv ;
		// 移交给父类
		parent::MainRun(& $arrArgv) ;

		$aSubController = $this->GetController('MVCContainer') ;
		$aSubController->MainRun(& $arrArgv) ;

		// 堵塞
		$this->Waiting() ;
	}


	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnCreateMainRunCode()
	{
		$nResponse = JCAT_Gtk::MsgDlg(JCAT_Language::Sentence("如果你已经重写了 MainRun() 方法中的代码，当前操作将会造成你的代码丢失。\r\n\r\n是否继续？")
			,'',Gtk::MESSAGE_QUESTION,Gtk::BUTTONS_YES_NO) ;
		if($nResponse!==Gtk::RESPONSE_YES)
		{
			return ;
		}
		
		$aDoc = $this->GetDocument('MainDoc') ;
		$sPath = $aDoc->GetPath() ;
		if(!is_file($sPath))
		{
			JCAT_Gtk::MsgDlg(JCAT_Language::Sentence('请先保存当前文档。')) ;
			return ;
		}
		
		$sCode = $aDoc->CreateCode_MainRun() ;
		
		CoderDocBase::WirteUnmodifiableBlock($sPath,'Main Run',$sCode,CoderDocBase::UnmodifiableBlockKind_Code,2) ;
	}
}


?>